﻿/*
 * Created by SharpDevelop.
 * User: IEscarro
 * Date: 9/2/2009
 * Time: 1:47 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using NHibernate;
using System.Collections.Generic;
using System.Data;
using Buaya.Model.Dao.Interface;

namespace Buaya.Model.Dao.NHibernate
{
	/// <summary>
	/// Description of NHibernateTimesheetDao.
	/// </summary>
	public class NHibernateTimesheetDao : ITimesheetDao
	{
		public NHibernateTimesheetDao()
		{
		}
		
		public void SaveOrUpdate(Timesheet sheet)
		{
			ISession session = NHibernateHelper.OpenSession();
			session.SaveOrUpdate(sheet);
			session.Flush();
		}
		
		public void Delete(Timesheet sheet)
		{
			ISession session = NHibernateHelper.OpenSession();
			session.Delete(sheet);
			session.Flush();
		}
		
		public Timesheet ReadByPk(int id)
		{
			return NHibernateHelper.OpenSession().Load<Timesheet>(id);
		}
		
		public IList<Timesheet> FindAll()
		{
			return NHibernateHelper.OpenSession().CreateCriteria(typeof(Timesheet)).List<Timesheet>();
		}
		
		public IList<Timesheet> FindByDate(DateTime from, DateTime to)
		{
			IQuery query = NHibernateHelper.OpenSession().CreateQuery("from Timesheet where SheetDate between :from and :to");
			query.SetDateTime("from", from);
			query.SetDateTime("to", to);
			return query.List<Timesheet>();
		}
	}
}
